package com.zp.self.module.工具.时间转换;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author ：byH
 * @date ：Created 2022/3/25 22:52
 * @description：简单查看LocalDateTime的Api
 * 参考：https://www.cnblogs.com/huanshilang/p/12013386.html
 */
public class LocalDateTime_JDK8 {

//        Assert.assertTrue(true);
    /**
    *  字符串 转 日期，通过DateTimeFormatter。
     *  将dtf传递给时间,而不是转换器操作时间
    **/
    @Test
    public  void String2Date(){
        //简单转换
        String dateStr = "2019/11/20 15:23:46";
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime localDateTime=LocalDateTime.parse(dateStr,dtf);
        System.out.println(localDateTime); // 2019-11-20T15:23:46

    }

    /**
     *  日期 转 字符串，通过DateTimeFormatter。
     *  将dtf传递给时间,而不是转换器操作时间
     **/
    @Test
    public  void Date2String(){
        //简单转换 自定义格式
        LocalDateTime now = LocalDateTime.now();
//        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String  dateStr=now.format(dtf);
        System.out.println(dateStr); // 2022/03/25 23:03:05

        //DateTimeFormatter 默认格式
        System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); //2022-03-26T08:27:27.585
        System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); //2022-03-26T08:27:27.585

        LocalDate localDate = LocalDate.of(2019, 9, 10);
        System.out.println(localDate.format(DateTimeFormatter.BASIC_ISO_DATE));//20190910
        System.out.println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));//2019-09-10

    }

    /**
     *  Description  LocalDate、LocalTime、LocalDateTime 的使用
     **/
    @Test
    public  void DateOther(){
        //1.LocalDateTime：获取年月日时分秒等于LocalDate+LocalTime
        LocalDateTime localDateTime0 = LocalDateTime.now();
        System.out.println(localDateTime0); // 2019-11-20T15:04:29.017
        LocalDate localDate = localDateTime0.toLocalDate();
        LocalTime localTime = localDateTime0.toLocalTime();
        LocalDateTime of = LocalDateTime.of(localDate, localTime);

        //（2）LocalDate：获取年月日
        LocalDate localDate1=LocalDate.now();
        System.out.println(localDate1); // 2019-11-20

        //（3）LocalTime：获取时分秒
        LocalTime localTime2 = LocalTime.now();
        System.out.println(localTime2); // 15:14:17.081
        int hour = localTime2.getHour();
        int minute = localTime2.getMinute();
        int second = localTime2.getSecond();
    }

    /**
     *  LocalDate、LocalTime、LocalDateTime、Instant为不可变对象，修改这些对象对象会返回一个副本。增加、减少年数、月数、天数等
     *  以LocalDateTime为例。
     **/
    @Test
    public  void DateApi(){
        // 创建日期：2019-09-10 14:46:56
        LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
        //增加一年
        localDateTime = localDateTime.plusYears(1);  //结果： 2020-09-10 14:46:56
        localDateTime = localDateTime.plus(1, ChronoUnit.YEARS); //结果： 2021-09-10 14:46:56
        //减少一个月
        localDateTime = localDateTime.minusMonths(1);  //结果： 2021-08-10 14:46:56
        localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS); //结果： 2021-07-10 14:46:56

        //修改年为2019
        localDateTime = localDateTime.withYear(2020);
        //修改为2022
        localDateTime = localDateTime.with(ChronoField.YEAR, 2022);
    }

    /**
     * 日期计算 TemporalAdjusters提供的各种日期时间格式化的静态类，比如firstDayOfYear是当前日期所属年的第一天
     **/
    @Test
    public  void Datesum(){
        LocalDate localDate = LocalDate.now();
        //当月第一天
        LocalDate localDate1 = localDate.with(TemporalAdjusters.firstDayOfMonth());
        //当月第最后一天天
        LocalDate localDate2 = localDate.with(TemporalAdjusters.lastDayOfMonth());
    }
}
